/*
Copyright (c) 2025 HeZongLun
NahidaProject is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan
PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/

#pragma once

#ifdef _WIN32
#define ExportSymbol  __declspec(dllexport)
#else
#define ExportSymbol
#endif

#include "XMLDocument.h"
#include "XMLElement.h"
#include "XMLComment.h"
#include "XMLText.h"

#include <iostream>
#include <string>
#include <string_view>

namespace NahidaProject {
	class ExportSymbol XMLParser {
	private:
		std::string_view xmlData_;
		size_t position_;

		bool IsWhitespace(char c) const;
		bool IsNameStartChar(char c) const;
		bool IsNameChar(char c) const;
		void SkipWhitespace();
		bool Match(const std::string_view& str);
		std::string ReadUntil(char delimiter);
		std::string ReadName();
		std::string DecodeEntity(const std::string& entity);
		std::string DecodeText(const std::string& text);
		std::string ParseAttributeValue();
		void ParseAttributes(XMLElement& element);
		void ParseProcessingInstruction();
		std::unique_ptr<XMLComment> ParseComment();
		std::unique_ptr<XMLText> ParseCData();
		std::unique_ptr<NahidaProject::XMLElement> ParseElement();

	public:
		explicit XMLParser(std::string_view xmlData) : xmlData_(xmlData), position_(0) {

		}

		XMLDocument Parse();
	};
}
